package com.yanqu.road.server.channelserver.manager.activity.entername;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.entername.ActivityGroupMatch;
import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.entity.activity.entername.compare.ChannelEnterNameUserDataCompare;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRank;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameGroupRankModel;
import com.yanqu.road.server.channelserver.manager.activity.entername.rank.EnterNameRank;

import java.math.BigInteger;
import java.util.*;

public abstract class TempEnterNameActivity implements EnterNameActivity {

    private ActivityInfo activityInfo;

    public TempEnterNameActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    /**
     * 是否有全地区榜单
     */
    protected boolean allAreaRank;

    public boolean inSameMatchGroup(ActivityGroupMatch groupMatch, int groupId) {
        if(groupId == 0){
            return false;
        }
        if(groupId == groupMatch.getGroupId() || groupId == groupMatch.getEnemyGroupId()){
            return true;
        }
        return false;
    }

    public boolean isAllAreaRank() {
        return allAreaRank;
    }

    public void setAllAreaRank(boolean allAreaRank) {
        this.allAreaRank = allAreaRank;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    protected void initGroupRankModel(Map<Integer, ActivityGroupMatch> matchMap, Map<Integer, BigInteger> groupValueMap, Map<Integer, Long> groupUpdateTimeMap) {
        Map<Integer, EnterNameGroupRankModel> matchRankModelMap = new HashMap<>();
        for (Map.Entry<Integer, ActivityGroupMatch> matchEntry : matchMap.entrySet()) {
            ActivityGroupMatch match = matchEntry.getValue();
            if (!matchRankModelMap.containsKey(match.getRankMatchGroupId())) {
                EnterNameGroupRankModel groupRankModel = new EnterNameGroupRankModel(getActivityInfo().getActivityId(), getGroupConditionType(), match.getRankMatchGroupId());
                groupRankModel.setRankList(new LinkedList<>());
                matchRankModelMap.put(match.getRankMatchGroupId(), groupRankModel);
            }
            EnterNameGroupRankModel model = matchRankModelMap.get(match.getRankMatchGroupId());

            BigInteger val = groupValueMap.getOrDefault(match.getGroupId(), BigInteger.ZERO);
            long updateTime = groupUpdateTimeMap.getOrDefault(match.getGroupId(), System.currentTimeMillis()) / 1000;
            EnterNameGroupRank groupRank = new EnterNameGroupRank(match.getGroupId(), val, getGroupConditionType(), updateTime, "");
            model.rankChange(groupRank);

            //全服
            //分组0
            if(isAllAreaRank()){
                EnterNameGroupRank allGroupRank = groupRank.clone();
                allGroupRank.setType(getAllGroupConditionType());
                ChannelEnterNameRankMgr.changeGroupValue(getActivityInfo().getActivityId(), getAllGroupConditionType(), 0, allGroupRank);
            }

            if (match.getEnemyGroupId() > 0) {
                BigInteger enemyVal = groupValueMap.getOrDefault(match.getEnemyGroupId(), BigInteger.ZERO);
                long lastUpdateTime = groupUpdateTimeMap.getOrDefault(match.getEnemyGroupId(), System.currentTimeMillis()) / 1000;
                EnterNameGroupRank groupRankEnemy = new EnterNameGroupRank(match.getEnemyGroupId(), enemyVal, getGroupConditionType(), lastUpdateTime, "");
                model.rankChange(groupRankEnemy);

                //全服
                //分组0
                if(isAllAreaRank()){
                    EnterNameGroupRank allGroupRankEnemy = groupRankEnemy.clone();
                    allGroupRankEnemy.setType(getAllGroupConditionType());
                    ChannelEnterNameRankMgr.changeGroupValue(getActivityInfo().getActivityId(), getAllGroupConditionType(), 0, allGroupRankEnemy);
                }
            }
        }

        if (matchRankModelMap.size() > 0) {
            ChannelEnterNameRankMgr.initGroupRank(getActivityInfo().getActivityId(), getGroupConditionType(), matchRankModelMap);
        }
    }

    public static List<Integer> getMatchGroupZoneList(Map<Integer, ActivityGroupMatch> matchMap, ActivityGroupMatch groupMatch) {
        List<Integer> zoneIdList = new ArrayList<>();
        zoneIdList.addAll(groupMatch.getZoneIdList());
        if(groupMatch.getEnemyGroupId() > 0){
            ActivityGroupMatch enemyGroup = matchMap.get(groupMatch.getEnemyGroupId());
            if(enemyGroup != null){
                zoneIdList.addAll(enemyGroup.getZoneIdList());
            }
        }
        return zoneIdList;
    }

    public void changeUserRank(ChannelEnterNameUserDataCompare userData, ActivityGroupMatch match, UserEnterName userEnterName){
        EnterNameRank rank = new EnterNameRank();
        rank.setUserId(userData.getUserId());
        rank.setGroupId(userEnterName.getGroupId());
        rank.setConditionType(getUserConditionType());
        rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
        rank.setValue(BigInteger.valueOf(userData.getUserRankValue()));
        rank.setParam(userData.getUserRankParam());
        rank.setUserBaseInfo(userEnterName.getUserBaseInfo());
        rank.setZoneId(userEnterName.getZoneId());
        ChannelEnterNameRankMgr.changeUserRank(getActivityInfo().getActivityId(), getUserConditionType(), match.getRankMatchGroupId(), rank);
    }

    public void changeGroupRank(ChannelEnterNameUserDataCompare userData, ActivityGroupMatch match, BigInteger total) {
        EnterNameGroupRank groupRank = new EnterNameGroupRank();
        groupRank.setType(getGroupConditionType());
        groupRank.setGroupId(match.getGroupId());
        groupRank.setValue(total);
        groupRank.setLastUpdateTime(userData.getLastUpdateTime2() / 1000);
        //本分组排行
        ChannelEnterNameRankMgr.changeGroupValue(getActivityInfo().getActivityId(), getGroupConditionType(), match.getRankMatchGroupId(), groupRank);

        //全国排行
        if(allAreaRank) {
            EnterNameGroupRank allGroupRank = groupRank.clone();
            allGroupRank.setType(getAllGroupConditionType());
            ChannelEnterNameRankMgr.changeGroupValue(getActivityInfo().getActivityId(), getAllGroupConditionType(), 0, allGroupRank);
        }
    }
}
